package designPattern.singleton;

public class SingletonTest1 {
    public static void main(String[] args) {
        Singleton1 instance1 = Singleton1.getInstance();
        Singleton1 instance2 = Singleton1.getInstance();

        //判断两个对象是否相同
        System.out.println(instance2==instance1);

        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance1.hashCode() = " + instance2.hashCode());
    }
}

//饿汉式（静态常量）

class Singleton1{
    //1.私有化构造器
    private Singleton1(){

    }

    //2.本类内部创建对象实例
    private final static Singleton1 instance = new Singleton1();

    //3.创建一个内部的静态方法，返回实例对象
    public static Singleton1 getInstance(){
        return instance;
    }
}